import 'package:bilibili/model/video_model.dart';
import 'package:bilibili/widget/video_card.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

import '../api/video.dart';
import '../dao/home_dao.dart';
import '../net/http/core/fw_error.dart';
import '../model/home_model.dart';
import '../util/color.dart';
import '../util/toast.dart';
import '../widget/banner.dart';

class HomeTabPage extends StatefulWidget {
  final String name;
  final List<HomeMo> bannerList;
  int tid = 0;

  HomeTabPage(
      {Key? key,
      required this.name,
      required this.bannerList,
      required this.tid})
      : super(key: key);

  @override
  _HomeTabPageState createState() => _HomeTabPageState();
}

class _HomeTabPageState extends State<HomeTabPage> {
  List<HomeMo> data = [];
  List<VideoModel> _videos = videos.toList();

  ScrollController _scrollController = ScrollController();
  int pageIndex = 1;
  bool _loading = false;
  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      var dis = _scrollController.position.maxScrollExtent -
          _scrollController.position.pixels;
      print('dis:$dis');
      //当距离底部不足300时加载更多
      if (dis < 300 && !_loading) {
        print('------_loadData---');
        _loadData(tid: widget.tid, loadMore: true);
      }
    });
    _loadData(tid: widget.tid);
  }

  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      onRefresh: _loadData,
      color: primary,
      child: MediaQuery.removePadding(
          removeTop: true,
          context: context,
          child: StaggeredGridView.countBuilder(
              controller: _scrollController,
              physics: const AlwaysScrollableScrollPhysics(),
              padding: EdgeInsets.only(top: 10, left: 10, right: 10),
              crossAxisCount: 2,
              itemCount: data.length,
              itemBuilder: (BuildContext context, int index) {
                //有banner时第一个item位置显示banner
                if (widget.bannerList != null && index == 0) {
                  return Padding(
                      padding: EdgeInsets.only(bottom: 8), child: _banner());
                } else {
                  return VideoCard(videoMo: _videos[index]);
                }
              },
              staggeredTileBuilder: (int index) {
                if (widget.bannerList != null && index == 0) {
                  return StaggeredTile.fit(2);
                } else {
                  return StaggeredTile.fit(1);
                }
                // return StaggeredTile.fit(2);
              })),
    );
  }

  _banner() {
    return Padding(
        padding: EdgeInsets.only(left: 5, right: 5),
        child: FwBanner(widget.bannerList!));
  }

  Future<void> _loadData({int tid = 0, loadMore = false}) async {
    if (!loadMore) {
      pageIndex = 1;
    }
    var currentIndex = pageIndex + (loadMore ? 1 : 0);
    try {
      List<HomeMo> homeDatas = await HomeDao.loadHomeRecommend(tid,
          pageIndex: currentIndex, pageSize: 10);
      setState(() {
        if (mounted) {
          //合并
          data = [...data, ...homeDatas];
          if (homeDatas != null) {
            pageIndex++;
          }
        }
      });
    } on NeedAuth catch (e) {
      print(e);
      showWarnToast(e.message);
    }
  }
}
